<?php 

function display_footer(){        
    $_SESSION['x']='asca';
    $block_1 = block_load('block', '1');
    $temp = _block_get_renderable_array(_block_render_blocks(array($block_1)));
    $output_1 = drupal_render($temp);
    $block_2 = block_load('block', '2');
    $temp =_block_get_renderable_array(_block_render_blocks(array($block_2)));
    $output_2 = drupal_render($temp);
    $block_3 = block_load('block', '3');
    $temp =_block_get_renderable_array(_block_render_blocks(array($block_3)));    
    $output_3 = drupal_render($temp);
    
    $s='<div id="about-us">';
        $s.='<div class="col col-1">'.$output_1.'</div>';
        $s.='<div class="col col-2">'.$output_2.'</div>';
        $s.='<div class="col col-3">'.$output_3.'</div>';
        
    $s.='</div>';
    return $s;
}

function display_favorite_products($tid = NULL){
    if($tid==NULL)
        $tid=arg(2);    
    $products = get_products_highest_rating($tid);
    if(empty($products))
        return '';
    
    $s='<div class="cblock" id="favorite-products">';
    drupal_add_js(drupal_get_path('module', 'cproduct') . '/js/scripts.js');
        $s.='<h2 class="block-title">Các sản phẩm được yêu thích nhất</h2>';
        $s.='<div class="col left">';
            $path = image_style_url('product_favorite_1', $products[0]->thumbnails[0]);
            $s.='<a href="'.url('node/'.$products[0]->nid).'"><img src="'.$path.'" class="first"/></a>';
            $s.='<div class="brand">'.$products[0]->brand['name'].'</div>';
            $s.='<div class="price">';
                $s.='<span class="sell-price">'.display_price($products[0]->sell_price).'</span>';
                if($products[0]->listed_price!=0){
                    $s.='<span class="listed-price">'.display_price($products[0]->listed_price).'</span>';
                }
            $s.='</div>';
            $s.='<div class=title><a href="'.url('node/'.$products[0]->nid).'">'.$products[0]->title.'</a></div>';
            $s.='<div class="btn btn-order" nid="'.$products[0]->nid.'">Mua hàng</div>';
        $s.='</div>';

        $s.='<div class="col right">';
        for($i=1; $i<9; $i++){
            if(isset($products[$i])){
                $path = image_style_url('product_favorite_2', $products[$i]->thumbnails[0]);
                $s.='<a href="'.url('node/'.$products[$i]->nid).'"><img src="'.$path.'" class="first"/></a>';                 
            }
        }
        $s.='</div>';

    $s.='</div>';
    return $s;
}


function display_price($price, $format='default'){
    if(is_numeric($price)){
        return number_format($price, 3, ".", ".").' VNĐ';        
    }
    return '';
}

/**
 * Display a comment in product detail page
 */
function display_comment($comment){
    $s='';
    //var_dump($comment);
    $s.='<div class="comment cblock">';
        $s.='<div class="col left">';
            $fid = ($comment->picture!=0)? $comment->picture:FID_NO_IMGAGE;
            $avatar = file_load($fid);
            $s.='<div class="user-avatar"><img src="'.image_style_url('user_avatar_comments', $avatar->uri).'" /></div>';
            $s.='<a href="'.url('user/'.$comment->uid).'" class="user-link">'.$comment->name.'</a>';
            $s.= '<div class="post-date">'.format_date($comment->created, 'custom', 'd/m/Y').'</div>';
        $s.='</div>';
        
        $s.='<div class="col right">';
            $s.='<h3 class="comment-title">'.$comment->subject.'</h3>';
            $s.='<div class="comment-body">'.$comment->comment_body[LANGUAGE_NONE][0]['value'].'</div>';
        $s.='</div>';
    $s.='</div>';
    return $s;
}

function display_sale_off_products($tid=NULL){    
     if($tid==NULL)
        $tid=arg(2);    
    $products = get_products_sale_off($tid);
    if(empty($products))
        return '';
    
    $s='<div class="cblock" id="sale-off-products">';
        $s.='<div class="col left">';
            $s.='<h2 class="block-title">Các sản phẩm SALE OFF</h2>';
            $query = array('saving', array(1, 99), 'category'=>$tid);            
            $s.='<span class="view-all"><a href="'.url('search-product', array('query'=>$query)).'">Xem tất cả &raquo;</a></span>';
            for($i=0; $i<4; $i++){
                if(isset($products[$i])){
                    $s.='<div class="product">';
                        $s.='<div class="col left">';
                            $s.='<a href="'.url('node/'.$products[$i]->nid).'"><img src="'.  image_style_url('product_sale_off', $products[$i]->thumbnails[0]).'" /></a>';
                        $s.='</div>';
                        $s.='<div class="col right">';
                            $s.='<div class="title"><a href="'.url('node/'.$products[$i]->nid).'">'.$products[$i]->title.'</a></div>';
                            $s.='<div class="brand"><a href="'.url('taxonomy/term/'.$products[$i]->brand['tid']).'">'.$products[$i]->brand['name'].'</a></div>';
                            if($products[$i]->saving && $products[$i]->saving>0){
                                $s.='<div class="saving">Giảm giá<div class="percent">'.$products[$i]->saving.'%</div></div>';
                            }
                            if($products[$i]->listed_price && $products[$i]->listed_price>0){
                                $s.='<div class="listed-price">'.display_price($products[$i]->listed_price).'</div>';
                            }
                            $s.='<div class="sell-price">'.display_price($products[$i]->sell_price).'</div>';
                            $s.='<div class="btn btn-order" nid="'.$products[$i]->nid.'">Mua hàng</div>';
                        $s.='</div>';
                    $s.='</div>';
                }
            }
        $s.='</div>';

        $s.='<div class="col right">';
            $s.='<h2 class="block-title">Xem sản phẩm theo</h2>';
            $s.='<div class="cblock-usage cblock">';
                $s.='<h4 class="filter-title">Mục đích sử dụng</h4>';
                $terms = taxonomy_get_tree($vid=VID_PRODUCT_USAGE);
                $query = array('category'=>$tid);
                foreach($terms as $term){
                    $query['usage']= $term->tid;
                    $s.='<span class="usage"><a href="'.url('search-product', array('query'=>$query)).'">'.$term->name.'</a></span>';
                } 
                
                $s.='<h4 class="filter-title">Màu sắc</h4>';                
                $info = field_info_field('field_color');
                $str_color = $info['settings']['default_colors'];
                $arr_color = explode(",", $str_color);
                $query = array('category'=>$tid); // reset query
                foreach($arr_color as $color){
                    $query['color']= $color;
                    $s.='<a href="'.url('search-product', array('query'=>$query)).'"><span class="color" style="background:'.$color.'"></span></a>';
                }
                
                $s.='<h4 class="filter-title">Chất liệu</h4>';
                $terms = taxonomy_get_tree($vid=VID_PRODUCT_MATERIAL);
                $query = array('category'=>$tid);
                foreach($terms as $term){
                    $query['material']= $term->tid;
                    $s.='<span class="material"><a href="'.url('search-product', array('query'=>$query)).'">'.$term->name.'</a></span>';
                } 
            $s.='</div>';
        $s.='</div>';

    $s.='</div>';
    return $s;
}


function display_products_in_category($tid=NULL){
    if($tid==NULL)
        $tid=arg(2);    
    $products = get_products_in_category($tid);
    if(empty($products))
        return '';
    
    $term_product_category = taxonomy_term_load($tid);
    $query = array('category'=>$tid);
     $s='<div class="cblock" id="all-products-by-category">';
        $s.='<h2 class="block-title">Tất cả các sản phẩm '.$term_product_category->name.'</h2>';
        $s.='<span class="view-all"><a href="'.url('search-product', array('query'=>$query)).'">Xem tất cả &raquo;</a></span>';
        foreach($products as $product){
            $s.='<div class="product col">';
                $s.='<div class="thumbnail"><a href="'.url('node/'.$product->nid).'"><img src="'.image_style_url('product_by_category', $product->thumbnails[0]).'" /></a></div>';
                if($product->saving > 0){
                    $s.='<span class="saving">'.$product->saving.'%</span>';
                }
                $s.='<div class="title"><a href="'.url('node/'.$product->nid).'">'.$product->title.'</a></div>';
                $s.='<div class="brand">'.$product->brand['name'].'</div>';
                $s.='<div class="price">';
                    $s.='<span class="sell-price">'.  display_price($product->sell_price).'</span>';
                if($products[0]->listed_price!=0){
                    $s.='<span class="listed-price">'.display_price($products[0]->listed_price).'</span>';
                }
                $s.='</div>';            
                $s.='<div class="btn btn-order" nid="'.$products[0]->nid.'">Mua hàng</div>';
            $s.='</div>';
        }
     $s.='</div>';
    return $s;
}

function display_shopping_page(){
    $s='';
    $terms = taxonomy_get_tree(VID_PRODUCT_CATEGORY, 0, 1, TRUE);
    foreach($terms as $term){        
        $s.=display_category_item($term);
    }
    return $s;
}

function display_category_item($term){
    $term = taxonomy_term_load($term->tid);
    $s='';
    $s.='<div class="term cblock cblock' . $term->tid . '">';
            $s.='<h2 class="block-title"><a href="'.url('taxonomy/term/'.$term->tid).'">'.$term->name.'</a></h2>';

            // Loại sản phẩm
            $s.='<div class="related-links">';
            $query = array('category'=>$term->tid);
            $query['sort'] = 'latest';
            $s.='<a class="lastest" href="'.url('search-product', array('query'=>$query)).'">Mới nhất</a>';
            $query['sort'] = 'lowestprice';
            $s.='<a class="lowestprice" href="'.url('search-product', array('query'=>$query)).'">Giá thấp nhất</a>';
            $query['sort'] = 'saving';
            $s.='<a class="saving" href="'.url('search-product', array('query'=>$query)).'">Khuyến mãi lớn nhất</a>';
            $query['sort'] = 'sticky';
            $s.='<a class="sticky" href="'.url('search-product', array('query'=>$query)).'">Nổi bật nhất</a>';
            $query['sort'] = 'rating';
            $s.='<a class="rating" href="'.url('search-product', array('query'=>$query)).'">Yêu thích nhất</a>';
            $s.='</div>';

            
            $s.='<div class="col thumbnail"><a href="'.url('taxonomy/term/'.$term->tid).'">';
            if(isset($term->field_thumbnail[LANGUAGE_NONE])){
                $s.='<img src="'.image_style_url('product_by_category', $term->field_thumbnail[LANGUAGE_NONE][0]['uri']).'" />';
            }else{
                $default_thumbnail = file_load(FID_NO_IMGAGE);
                $s.='<img src="'.image_style_url('product_by_category', $default_thumbnail->uri).'" />';
            }
            $s.='</a></div>';
            
            // Danh mục
            $s.='<div class="col">';
                $s.='<div class="colBlock categories">';
                    $s.='<div class="col-title">Danh mục</div>';
                    $s.='<div class="col-content">';
                        $children_terms = taxonomy_get_children($term->tid);
                        foreach($children_terms as $child){
                            $s.='<a href="'.url('taxonomy/term/'.$child->tid).'">'.$child->name.'</a>';
                        }
                    $s.='</div>';
                $s.='</div>';
            $s.='</div>';
            
            // Thương hiệu cao cấp
            $s.='<div class="col">';
                $s.='<div class="colBlock brands">';
                    $s.='<div class="col-title">Thương hiệu cao cấp</div>';
                    $s.='<div class="col-content">';
                        $query = array('category'=>$term->tid);
                        $brand_ids = get_brand_by_category($term->tid);
                        foreach($brand_ids as $brand_id){
                            $brand = taxonomy_term_load($brand_id);
                            $query['brand'] = $brand_id;
                            $s.='<a href="'.url('search-product', array('query'=>$query)).'">'.$brand->name.'</a>';
                        }
                    $s.='</div>';
                $s.='</div>';
            $s.='</div>';

            // Mục đích sử dụng
            $s.='<div class="col">';
                $s.='<div class="colBlock usage">';
                    $s.='<div class="col-title">Mục đích sử dụng</div>';
                    $s.='<div class="col-content">';
                        $query = array('category'=>$term->tid);
                        $children_terms = taxonomy_get_tree(VID_PRODUCT_USAGE);
                        foreach($children_terms as $child){
                            $query['usage']=$child->tid;
                            $s.='<a href="'.url('search-product', array('query'=> $query)).'">'.$child->name.'</a>';
                        }
                    $s.='</div>';
                $s.='</div>';
            $s.='</div>';

             // Giá bán
            $s.='<div class="col">';
                $s.='<div class="colBlock sell-price">';
                    $s.='<div class="col-title">Giá bán</div>';
                    $s.='<div class="col-content">';
                        $query = array('category'=>$term->tid);
                        $query['price']=array(0, 200);
                        $s.='<a href="'.url('search-product', array('query'=>$query)).'">Dưới 200k</a>';
                        $query['price']=array(200, 400);
                        $s.='<a href="'.url('search-product', array('query'=>$query)).'">200k - 400k</a>';
                        $query['price']=array(400, 600);
                        $s.='<a href="'.url('search-product', array('query'=>$query)).'">400k - 600k</a>';
                        $query['price']=array(600, 800);
                        $s.='<a href="'.url('search-product', array('query'=>$query)).'">600k - 800k</a>';
                        $query['price']=array(800, 9999999);
                        $s.='<a href="'.url('search-product', array('query'=>$query)).'">Trên 800k</a>';
                    $s.='</div>';
                $s.='</div>';
            $s.='</div>';
            

            
        $s.='</div>';
        return $s;
}

function display_category_item_in_menu($term){
    $term = taxonomy_term_load($term->tid);
    $s='';
    $s.='<div class="term cblock">';
    $s.='<h2 class="block-title"><a href="'.url('taxonomy/term/'.$term->tid).'">'.$term->name.'</a></h2>';


    /*$s.='<div class="col thumbnail"><a href="'.url('taxonomy/term/'.$term->tid).'">';
    if(isset($term->field_thumbnail[LANGUAGE_NONE])){
        $s.='<img src="'.image_style_url('product_by_category', $term->field_thumbnail[LANGUAGE_NONE][0]['uri']).'" />';
    }else{
        $default_thumbnail = file_load(FID_NO_IMGAGE);
        $s.='<img src="'.image_style_url('product_by_category', $default_thumbnail->uri).'" />';
    }
    $s.='</a></div>';*/

    // Danh mục
    $s.='<div class="col">';
    $s.='<div class="colBlock categories">';
    $s.='<div class="col-title">Danh mục</div>';
    $s.='<div class="col-content">';
    $children_terms = taxonomy_get_children($term->tid);
    foreach($children_terms as $child){
        $s.='<a href="'.url('taxonomy/term/'.$child->tid).'">'.$child->name.'</a>';
    }
    $s.='</div>';
    $s.='</div>';
    $s.='</div>';

    // Thương hiệu cao cấp
    $s.='<div class="col">';
    $s.='<div class="colBlock brands">';
    $s.='<div class="col-title">Thương hiệu cao cấp</div>';
    $s.='<div class="col-content">';
    $query = array('category'=>$term->tid);
    $brand_ids = get_brand_by_category($term->tid);
    foreach($brand_ids as $brand_id){
        $brand = taxonomy_term_load($brand_id);
        $query['brand'] = $brand_id;
        $s.='<a href="'.url('search-product', array('query'=>$query)).'">'.$brand->name.'</a>';
    }
    $s.='</div>';
    $s.='</div>';
    $s.='</div>';

    // Mục đích sử dụng
    $s.='<div class="col">';
    $s.='<div class="colBlock usage">';
    $s.='<div class="col-title">Mục đích sử dụng</div>';
    $s.='<div class="col-content">';
    $query = array('category'=>$term->tid);
    $children_terms = taxonomy_get_tree(VID_PRODUCT_USAGE);
    foreach($children_terms as $child){
        $query['usage']=$child->tid;
        $s.='<a href="'.url('search-product', array('query'=> $query)).'">'.$child->name.'</a>';
    }
    $s.='</div>';
    $s.='</div>';
    $s.='</div>';

    // Loại sản phẩm
    $s.='<div class="col">';
    $s.='<div class="colBlock type">';
    $s.='<div class="col-title">Loại sản phẩm</div>';
    $s.='<div class="col-content">';
    $s.='<div class="related-links">';
    $query = array('category'=>$term->tid);
    $query['sort'] = 'latest';
    $s.='<a href="'.url('search-product', array('query'=>$query)).'">Mới nhất</a>';
    $query['sort'] = 'lowestprice';
    $s.='<a href="'.url('search-product', array('query'=>$query)).'">Giá thấp nhất</a>';
    $query['sort'] = 'saving';
    $s.='<a href="'.url('search-product', array('query'=>$query)).'">Khuyến mãi lớn nhất</a>';
    $query['sort'] = 'sticky';
    $s.='<a href="'.url('search-product', array('query'=>$query)).'">Nổi bật nhất</a>';
    $query['sort'] = 'rating';
    $s.='<a href="'.url('search-product', array('query'=>$query)).'">Yêu thích nhất</a>';
    $s.='</div>';
    $s.='</div>';
    $s.='</div>';
    $s.='</div>';

    // Giá bán
    $s.='<div class="col">';
    $s.='<div class="colBlock sell-price">';
    $s.='<div class="col-title">Giá bán</div>';
    $s.='<div class="col-content">';
    $query = array('category'=>$term->tid);
    $query['price']=array(0, 200);
    $s.='<a href="'.url('search-product', array('query'=>$query)).'">Dưới 200k</a>';
    $query['price']=array(200, 400);
    $s.='<a href="'.url('search-product', array('query'=>$query)).'">200k - 400k</a>';
    $query['price']=array(400, 600);
    $s.='<a href="'.url('search-product', array('query'=>$query)).'">400k - 600k</a>';
    $query['price']=array(600, 800);
    $s.='<a href="'.url('search-product', array('query'=>$query)).'">600k - 800k</a>';
    $query['price']=array(800, 9999999);
    $s.='<a href="'.url('search-product', array('query'=>$query)).'">Trên 800k</a>';
    $s.='</div>';
    $s.='</div>';
    $s.='</div>';



    $s.='</div>';
    return $s;
}

 function display_block_cart() {
    //unset($_SESSION[SESSION_CART]);
    $s = '<h4>Giỏ hàng</h4>';

    $empty = '<div class="content">Giỏ hàng của bạn hiện chưa có sản phẩm nào</div>';
    
    if (!isset($_SESSION[SESSION_CART]) || empty($_SESSION[SESSION_CART])) {
        return $s.$empty;
    } else {        
        $orders = unserialize($_SESSION[SESSION_CART]);
        if(empty($orders)){
            return $s.$empty;
        }
        $total_cost = 0;
        $header = array('Sản phẩm', 'Tên SP', 'Số lượng', 'Đơn giá','Xóa');
        $rows = array();
        foreach ($orders as $order_detail) {  
            $product_nid = $order_detail->ref_nid;
            $product = new Product($product_nid);
            $thumbnail = image_style_url('thumbnail', $product->thumbnails[0]);            
            $rows[] = array(
                'thumbnail' => array('data'=>'<img src= "'.$thumbnail.'" />', 'class'=>'thumbnail'),
                'title' => array('data'=>'<a href="'.url('node/'.$product_nid).'">'.$product->title.'</a>', 'class'=>'title'),
                'amount'=> array('data'=>$order_detail->amount, 'class'=>'amount'),
                'price' => array('data'=>  display_price($product->sell_price), 'class'=>'price'),                
                'remove'=> array('data'=>'<span class="btn btn-remove" pnid="'.$product_nid.'">Xóa</span>', 'class'=>'remove'),
            );
            
            $total_cost+= $product->sell_price*$order_detail->amount;
        }
        $s.='<div id="total-cost">Tổng cộng: <span class="price">'.display_price($total_cost).'</span><a class="btn" id="btn-pay" href="'.url('payment').'">Thanh toán</a></div>';
        $s .= '<div class="content">';
        $s.=theme('table', array('header'=>$header, 'rows'=>$rows, 'attributes'=>array('id'=>'ordered-products')));
        

        $s.='</div>';
    }
    return $s;
}

function display_page_cart(){
    $s = '';
    if (!isset($_SESSION[SESSION_CART]) || empty($_SESSION[SESSION_CART])) {
        $s.='Giỏ hàng của bạn hiện chưa có sản phẩm nào';
    } else {
        $orders = unserialize($_SESSION[SESSION_CART]);
        $total_cost = 0;
        $header = array('Sản phẩm', '', 'Mã sản phẩm', 'Màu sắc', 'Kích cỡ', 'Số lượng', 'Đơn Giá', 'Thành tiền', 'Xóa');
        $rows = array();
        foreach ($orders as $order_detail) {  
            $product_nid = $order_detail->ref_nid;
            $product = new Product($product_nid);
            $thumbnail = image_style_url('thumbnail', $product->thumbnails[0]);            
            $rows[] = array(
                'thumbnail' => array('data'=>'<img src= "'.$thumbnail.'" />', 'class'=>'thumbnail'),
                'title' => array('data'=>'<a href="'.url('node/'.$product_nid).'">'.$product->title.'</a>', 'class'=>'title'),
                'id'    => array('data'=>$product->id, 'class'=>'id'),
                'color'    => array('data'=>'<span class="color" style="background:'.$order_detail->color.'"></span>', 'class'=>'o-color'),
                'size'    => array('data'=>display_product_select_box_size($product, $order_detail->size), 'class'=>'o-size'),
                'amount'=> array('data'=>'<input type="text" class="input-amount" id="input-amount-'.$product->nid.'" nid="'.$product->nid.'" value="'.$order_detail->amount.'" />', 'class'=>'amount'),
                'price' => array('data'=>  display_price($product->sell_price), 'class'=>'price', 'nid'=>$product->nid, 'price'=>$product->sell_price),                
                'row_price' => array('data'=>display_price($product->sell_price * $order_detail->amount), 'class'=>'price row_price', 'nid'=>$product->nid),
                'remove'=> array('data'=>'<span class="btn btn-remove" pnid="'.$product_nid.'">Remove</span>', 'class'=>'remove'),
            );
            
            $total_cost+= $product->sell_price*$order_detail->amount;
        }         
        $s.=theme('table', array('header'=>$header, 'rows'=>$rows, 'attributes'=>array('id'=>'ordered-products')));
        
        $s.='<div id="total-cost">Tổng cộng: <span class="price">'.display_price($total_cost).'</span><div class="btn" id="btn-pay"><a href="'.url('payment').'">Thanh toán</a></div></div>';
    }
    return $s;
}

function display_product_select_box_size($product, $seleted_size){
    $s='<select id="input-size-'.$product->nid.'" class="input-size" nid="'.$product->nid.'">';
    foreach($product->size as $size){
        if($seleted_size==$size)
            $s.='<option selected="selected">'.$size.'</option>';
        else
            $s.='<option>'.$size.'</option>';
    }
    $s.='</select>';
    return $s;
}


function display_page_payment(){
    process_add_order();
    $s='';
    $s.='<div class="step step-1">';
        $s.='<div class="step-title">Bước 1: Đăng nhập/ Đăng ký<span class="change-step btn">Thay đổi</span></div>';
        if(user_is_logged_in()){
            $s.='<div class="step-content">';
                $s.='Bạn đã đăng nhập với tài khoản: '.$GLOBALS['user']->mail;
                $s.='<br />Click vào "tiếp tục thanh toán" để chuyển qua "Bước 2: Thông tin người mua."';
                $s.='<div class="next-step btn" next="2">Tiếp tục thanh toán</div>';
            $s.='</div>';
        }else{
            $s.='<div class="step-content active">';
                $s.='<div class="col left">';
                    $s.='<b>Mua hàng không cần tài khoản</b>';
                    $s.='Bạn có thể tiếp tục mua hàng mà không cần đăng ký tài khoản. Hệ thống sẽ tự động tạo tài khoản nhanh giúp bạn quản lý đơn hàng.';             
                $s.='</div>';
                $s.='<div class="col left">';
                    $s.='<b>Đăng nhập</b>';
                    $form_login = drupal_get_form('user_login_block');
                    $s.=drupal_render($form_login);
                $s.='</div>';
                $s.='<div class="next-step btn" next="2">Tiếp tục thanh toán</div>';
            $s.='</div>';
        }        
    $s.='</div>';
    
    $s.='<form action="" method="post">';
    $s.='<div class="step step-2">';
        $s.='<div class="step-title">Bước 2: Thông tin người mua<span class="change-step btn">Thay đổi</span></div>';
        $s.='<div class="step-content';  if(user_is_logged_in()){ $s.=" active"; } $s.='">';    
            $s.=display_form_client_info('buyer');
            $s.='<input type="radio" name="transform" value="1" id="transform-1" checked="checked"/> Gửi tới địa chỉ này';
            $s.='<input type="radio" name="transform" value="2" id="transform-2" /> Gửi tới địa chỉ khác';
            $s.='<div class="next-step btn" next="4">Tiếp tục thanh toán</div>';
        $s.='</div>';
    $s.='</div>';
    
    $s.='<div class="step step-3">';
        $s.='<div class="step-title">Bước 3: Thông tin vận chuyển<span class="change-step btn">Thay đổi</span></div>';            
        $s.='<div class="step-content">';
            $s.=display_form_client_info('receive');
            $s.='<div class="next-step btn" next="4">Tiếp tục thanh toán</div>';
        $s.='</div>';
    $s.='</div>';
    
    $s.='<div class="step step-4">';
        $s.='<div class="step-title">Bước 4: Hình thức thanh toán<span class="change-step btn">Thay đổi</span></div>';            
        $s.='<div class="step-content">';
            $s.='<input type="radio" name="payment-type" value="Giao tiền nhận hàng" checked="checked"/> Giao hàng nhận tiền';
            $s.='<input type="radio" name="payment-type" value="Thanh toán qua ngân hàng"/> Thanh toán qua ngân hàng';
            $s.='<input type="submit" class="btn" id="btn-review" name="btnSubmit" value="Xác nhận"/>';
        $s.='</div>';
    $s.='</div>';
    $s.='</form>';
    return $s;
}

/**
 * Form: Thông tin người mua / thông tin vận chuyển
 */
function display_form_client_info($info_type){
    $s='<div class="form-client-info" id="form-'.$info_type.'-info">';
        $s.='<div class="col left">';
            $s.='<div class="row"><label>Họ tên*</label><input type="text" class="name" id="name-'.$info_type.'" name="name-'.$info_type.'" /></div>';
            $s.='<div class="row"><label>Số điện thoại*</label><input type="text" class="phone" id="phone-'.$info_type.'" name="phone-'.$info_type.'" /></div>';
            $s.='<div class="row"><label>Email*</label><input type="text" class="email" id="email-'.$info_type.'" name="email-'.$info_type.'" /></div>';
        $s.='</div>';     
        
        $s.='<div class="col right">';
            $s.='<div class="row"><label>Tỉnh/ Thành*</label><select class="city" id="city-'.$info_type.'" name="city-'.$info_type.'"><option value="Hồ Chí Minh">Hồ Chí Minh</option></select></div>';
            $s.='<div class="row"><label>Quận/ Huyện*</label><select class="district" id="district-'.$info_type.'" name="district-'.$info_type.'">';
            $arr_district = get_arr_district_in_HCM();
            foreach($arr_district as $district){
                $s.='<option value="'.$district.'" >'.$district.'</option>';
            }
            $s.='</select></div>';
            $s.='<div class="row"><label>Địa chỉ*</label><input type="text" class="address" id="address-'.$info_type.'" name="address-'.$info_type.'" /></div>';
        $s.='</div>';    
    $s.='</div>';
    return $s;
}

function display_page_search(){    
    $products = search_product(12);
    $query = drupal_get_query_parameters();
    
    $s='';
    $s.='<div class="col left">';
        $s.=display_block_filter_category();
        $s.=display_block_filter_price();
        $s.=display_block_filter_saving();
        $s.=display_block_filter_brand();        
    $s.='</div>';
    
    
    $s.='<div class="col right">';
        $s.='<div id="search-result">Kết quả tìm kiếm | <span class="search-counter">Tìm thấy '.search_product().' sản phẩm</div>';
         $s.='<div class="related-links">';
                $query['sort'] = 'latest';
                $s.='<a href="'.url('search-product', array('query'=>$query)).'">Mới nhất</a>';
                $query['sort'] = 'lowestprice';
                $s.='<a href="'.url('search-product', array('query'=>$query)).'">Giá thấp nhất</a>';
                $query['sort'] = 'saving';
                $s.='<a href="'.url('search-product', array('query'=>$query)).'">Khuyến mãi lớn nhất</a>';
                $query['sort'] = 'sticky';
                $s.='<a href="'.url('search-product', array('query'=>$query)).'">Nổi bật nhất</a>'; 
                $query['sort'] = 'rating';
                $s.='<a href="'.url('search-product', array('query'=>$query)).'">Yêu thích nhất</a>';                
         $s.='</div>';
         
         $s.=theme('pager');
         $s.='<div id="products-found">';
            foreach($products as $product){
                $s.='<div class="product col">';
                    $s.='<div class="thumbnail"><a href="'.url('node/'.$product->nid).'"><img src="'.  image_style_url('found_product', $product->thumbnails[0]).'" /></a></div>';
                    $s.='<div class="title"><a href="'.url('node/'.$product->nid).'">'.$product->title.'</a></div>';
                    $s.='<div class="price">'.  display_price($product->sell_price).'</div>';
                    $s.='<div class="btn btn-order" nid="'.$product->nid.'">Mua hàng</div>';
                $s.='</div>';
            }
         $s.='</div>';
         
         $s.=theme('pager');
    $s.='</div>';
    
    $s.='<div class="clearfix"></div>';
    return $s;
}

function display_block_filter_category(){
    $s='';
    $s.='<div class="cblock filter" id="filter-category">';
        $s.='<h2 class="block-title">Danh mục</h2>';
        $terms = taxonomy_get_tree(VID_PRODUCT_CATEGORY, 0, 1);
        $query = drupal_get_query_parameters();
        unset($query['category']);
        foreach($terms as $term){
            $s.='<div class="dep-1"><a href="'.url('taxonomy/term/'.$term->tid, array('query'=>$query)).'">'.$term->name.'</a></div>';    
            $children = taxonomy_get_children($term->tid);
            foreach($children as $child){
                $s.='<div class="dep-2"><a href="'.url('taxonomy/term/'.$child->tid, array('query'=>$query)).'">'.$child->name.'</a></div>';                    
            }
        }
    $s.='</div>';
    return $s;
}

function display_block_filter_price(){
    $s='';
     $s.='<div class="cblock filter" id="filter-price">';
        $s.='<h2 class="block-title">Giá bán</h2>';
        $query = drupal_get_query_parameters();        
        $query['price'] = array(0, 200);
        $s.='<div class="dep-1"><a href="'.url('search-product', array('query'=>$query)).'">Dưới 200k</a></div>';
        $query['price'] = array(200, 400);
        $s.='<div class="dep-1"><a href="'.url('search-product', array('query'=>$query)).'">200-400k</a></div>';
        $query['price'] = array(400, 600);
        $s.='<div class="dep-1"><a href="'.url('search-product', array('query'=>$query)).'">400-600k</a></div>';
        $query['price'] = array(600, 800);
        $s.='<div class="dep-1"><a href="'.url('search-product', array('query'=>$query)).'">600-800k</a></div>';
        $query['price'] = array(800, 99999);
        $s.='<div class="dep-1"><a href="'.url('search-product', array('query'=>$query)).'">Trên 800k</a></div>';
        
    $s.='</div>';
    return $s;
}

function display_block_filter_saving(){
    $s='';
    $s.='<div class="cblock filter" id="filter-saving">';
        $s.='<h2 class="block-title">Khuyến mãi</h2>';
        $query = drupal_get_query_parameters();        
        $query['saving'] = array(0, 10);
        $s.='<div class="dep-1"><a href="'.url('search-product', array('query'=>$query)).'">Dưới 10%</a></div>';
        $query['saving'] = array(10, 20);
        $s.='<div class="dep-1"><a href="'.url('search-product', array('query'=>$query)).'">10% - 20%</a></div>';
        $query['saving'] = array(20, 30);
        $s.='<div class="dep-1"><a href="'.url('search-product', array('query'=>$query)).'">20% - 30%</a></div>';
        $query['saving'] = array(30, 40);
        $s.='<div class="dep-1"><a href="'.url('search-product', array('query'=>$query)).'">30% - 40%</a></div>';
        $query['saving'] = array(40, 50);
        $s.='<div class="dep-1"><a href="'.url('search-product', array('query'=>$query)).'">40% - 50%</a></div>';
        $query['saving'] = array(50, 100);
        $s.='<div class="dep-1"><a href="'.url('search-product', array('query'=>$query)).'">Trên 50%</a></div>';
    $s.='</div>';
    return $s;
}

function display_block_filter_brand(){
    $s='';
    $s.='<div class="cblock filter" id="filter-brand">';
        $s.='<h2 class="block-title">Thương hiệu</h2>';
        $terms = taxonomy_get_tree(VID_PRODUCT_BRAND, 0, 1);
        $query = drupal_get_query_parameters();
        foreach($terms as $term){
            $query['brand'] = $term->tid;
            $s.='<div class="dep-1"><a href="'.url('search-product', array('query'=>$query)).'">'.$term->name.'</a></div>';    
        }
    $s.='</div>';
    return $s;
}

function display_page_full_order(){
    $order=node_load(arg(2));
    if(!$order)
        return 'Đơn hàng không tồn tại';
    
    $s='<h1>'.$order->title.'</h1>';
    $s.='<br /><hr />';
    $s.='<h3>Chi tiết</h3>';
    $s.=views_embed_view('order_detail', 'block_1', $order->nid);
    $s.='<b>Tổng giá</b>: '.display_price($order->field_total_cost[LANGUAGE_NONE][0]['value']);
            
    $s.='<br /><hr />';
    $s.='<h3>Thông tin người đặt hàng</h3>';
    $s.='<div><b>Họ tên</b>: '.$order->field_full_name[LANGUAGE_NONE][0]['value'].'</div>';
    $s.='<div><b>Điện thoại</b>: '.$order->field_phone[LANGUAGE_NONE][0]['value'].'</div>';
    $s.='<div><b>Email</b>: '.$order->field_email[LANGUAGE_NONE][0]['value'].'</div>';
    $s.='<div><b>Tỉnh thành</b>: '.$order->field_city[LANGUAGE_NONE][0]['value'].'</div>';
    $s.='<div><b>Quận huyện</b>: '.$order->field_district[LANGUAGE_NONE][0]['value'].'</div>';
    $s.='<div><b>Địa chỉ</b>: '.$order->field_address[LANGUAGE_NONE][0]['value'].'</div>';
    
    $s.='<br /><hr />';
    $s.='<h3>Thông tin người nhận</h3>';
    if($order->field_buy_as_receiver[LANGUAGE_NONE][0]['value']){
        $s.='<div>Cùng thông tin với người đặt hàng</div>';
    }else{
        $s.='<div><b>Họ tên</b>: '.$order->field_full_name_receive[LANGUAGE_NONE][0]['value'].'</div>';
        $s.='<div><b>Điện thoại</b>: '.$order->field_phone_receive[LANGUAGE_NONE][0]['value'].'</div>';
        $s.='<div><b>Email</b>: '.$order->field_email_receive[LANGUAGE_NONE][0]['value'].'</div>';
        $s.='<div><b>Tỉnh thành</b>: '.$order->field_city_receive[LANGUAGE_NONE][0]['value'].'</div>';
        $s.='<div><b>Quận huyện</b>: '.$order->field_district_receive[LANGUAGE_NONE][0]['value'].'</div>';
        $s.='<div><b>Địa chỉ</b>: '.$order->field_address_receive[LANGUAGE_NONE][0]['value'].'</div>';
    }
    return $s;
}


function display_custom_menu() {    
    echo '<ul id="ul-sub-main-menu" class="links inline clearfix">';
    $categories = taxonomy_get_tree(VID_PRODUCT_CATEGORY, 0, 1);    
    foreach ($categories as $term) {
        $active = is_active($term->tid)? " active":"";
        echo '<li catid="' . $term->tid . '" class="item'.$active.'"><a  class="item-link'.$active.'" href="'.url('taxonomy/term/'.$term->tid).'">' . $term->name . '</a>';
        echo '<div class="hid-catid-item" id="hid-catid-item-'.$term->tid.'">'.display_category_item_in_menu($term).'</div>';
        echo '</li>';
    }
    echo '</ul>';
}

function is_active($catid){
    if(arg(0)=='taxonomy' && arg(1)=='term'){
        if(arg(2) == $catid)
            return true;            
    }
    if(arg(0)=='node'){
        $product = new Product(arg(1));
        if(!$product)
            return false;
        $childrens_tid = get_children_tid($catid);
        if(in_array($product->category['tid'], $childrens_tid))
            return true;
    }
    return false;
}